﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

using WXWorkSvc.Common.Data;

namespace WXWorkSvc.Areas.Admin.Pages.Suite
{
    public class EditModel : PageModel
    {
        private readonly WXWorkSvc.Common.Data.WXDbContext _context;

        public EditModel(WXWorkSvc.Common.Data.WXDbContext context)
        {
            _context = context;
        }

        [BindProperty]
        public TSuite TSuite { get; set; } = default!;

        public async Task<IActionResult> OnGetAsync(string id)
        {
            var tsuite = await _context.Suites.FindAsync(id);
            TSuite = tsuite!;
            return Page();
        }

        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            var url = TSuite.Url?.Trim() ?? "";
            await _context.Suites.Where(ii => ii.Id == TSuite.Id).ExecuteUpdateAsync(ii => ii.SetProperty(jj => jj.Name, TSuite.Name).SetProperty(jj => jj.Secret, TSuite.Secret.Trim()).SetProperty(jj => jj.Url, url));

            return RedirectToPage("./Index");
        }

    }
}
